﻿using System;
using System.Diagnostics;

namespace Opo.Perspective.Core.PipelinePlugins
{
    /// <summary>
    /// PipelinePlugin that detects anti spam headers and adds the Spam-Tag to the message
    /// </summary>
    public class TagSpamPlugin : IPerspectivePipelinePlugin<Message>
    {
        /// <summary>
        /// Processes the specified PerspectiveMailMessage.
        /// </summary>
        /// <param name="perspectiveMailMessage">The perspective mail message.</param>
        public void Process(ref Message perspectiveMailMessage)
        {
            bool isSpam = false;

            // check for X-Spam-Status header
            string xSpamStatus = perspectiveMailMessage.Headers.GetValue("X-Spam-Status");
            Debug.WriteLine("X-Spam-Status: " + xSpamStatus);
            if (!String.IsNullOrEmpty(xSpamStatus.Trim()))
            {
                if (xSpamStatus.Trim().ToLower().StartsWith("yes"))
                {
                    isSpam = true;
                }
            }

            // check for X-GMX-Antispam header
            string gmx = perspectiveMailMessage.Headers.GetValue("X-GMX-Antispam");
            if (!String.IsNullOrEmpty(gmx.Trim()))
            {
                if (gmx.Trim().StartsWith("5") || gmx.Trim().StartsWith("6"))
                {
                    isSpam = true;
                }
            }

            if (isSpam)
            {
                perspectiveMailMessage.Tags.Add(SystemTags.Spam);
            }
        }
    }
}
